package com.liinxin.flac_management.page;

import com.liinxin.flac_management.controller.TxtMatchController;
import com.liinxin.flac_management.entity.FileTextField;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class DuplicatesUI {
    private Stage primaryStage;
    private Scene startScene;

    private TxtMatchController txtMatchController = new TxtMatchController();

    public DuplicatesUI() {
    }

    public DuplicatesUI(Stage primaryStage, Scene startScene) {
        this.primaryStage = primaryStage;
        this.startScene = startScene;
    }

    public Scene getScene() {
        // 创建一个标签
        Label label = new Label("txt文本去重");
        label.setFont(new Font("Arial", 20));

        // 输入框设置
        Label txtLabel = new Label("请输入文本地址: ");
        txtLabel.setFont(new Font("Arial", 20));
        FileTextField txtPathInput = new FileTextField();
        txtPathInput.setFont(new Font("Arial", 20));

        // 添加图片按钮
        Button duplicatesButton = new Button("文本去重");
        duplicatesButton.setFont(new Font("Arial", 20));
        // 清空地址按钮
        Button clearButton = new Button("清空地址");
        clearButton.setFont(new Font("Arial", 20));
        // 创建一个返回按钮
        Button backButton = new Button("返回主页");
        backButton.setFont(new Font("Arial", 20));

        // 设置文本展示框
        Label textLabel = new Label("去重后内容如下");
        textLabel.setFont(new Font("Arial", 20));
        HBox textLabelBox = new HBox(textLabel);
        textLabelBox.setAlignment(Pos.CENTER);

        TextArea textArea = new TextArea();
        textArea.setFont(new Font("Arial", 20));
        ScrollPane scrollPane = new ScrollPane(textArea);
        scrollPane.setFitToWidth(true);
        scrollPane.setFitToHeight(true);

        // textArea.setText("wangxiaomei\r\n520");

        HBox txtBox = new HBox(scrollPane);
        txtBox.setAlignment(Pos.CENTER);
        txtBox.setPadding(new Insets(10));

        // 创建hbox管理按钮
        HBox buttonBox = new HBox(40);
        buttonBox.setAlignment(Pos.CENTER);
        // 将按钮添加到HBox布局管理器中
        buttonBox.getChildren().addAll(duplicatesButton, clearButton, backButton);

        // 添加事件
        // 文本去重
        duplicatesButton.setOnAction(event -> {
            System.out.println("文本去重");
            System.out.println("文本地址: " + txtPathInput.getText());

            try {
                // 去重方法
                String duplicates = txtMatchController.duplicates(txtPathInput.getText());

                textArea.setText(duplicates);

                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setTitle("成功");
                alert.setHeaderText("去重成功");
                // 显示提示框并等待用户关闭
                alert.showAndWait();

            } catch (Exception e) {
                // 显示错误提示框
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("错误");
                alert.setHeaderText("路径错误");
                // 显示提示框并等待用户关闭
                alert.showAndWait();
                // 清空输入框数据
                txtPathInput.setText("");
            }

        });
        // 清空地址
        clearButton.setOnAction(event -> {
            System.out.println("清空地址");
            txtPathInput.setText("");
            textArea.setText("");

        });
        // 返回主页
        backButton.setOnAction(event -> {
            System.out.println("返回主页");
            primaryStage.setScene(startScene);
            primaryStage.show();
        });

        // 创建一个VBox布局管理器作为根布局
        VBox root = new VBox(15);
        root.getChildren().addAll(label, txtLabel, txtPathInput, buttonBox, textLabel, txtBox);

        // 创建一个场景并返回
        Scene scene = new Scene(root, 500, 500);
        return scene;
    }
}
